package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.bike.dto.MemberLocationForm;
import cn.com.chengmandian.bike.mapper.EbOperationAreaMapper;
import cn.com.chengmandian.bike.model.vo.OperationAreaVo;
import cn.com.chengmandian.bike.service.IEbOperationAreaService;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.pojo.BikeAreaParam;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "运营区相关接口")
@RestController
@RequestMapping("/operation/area")
public class EbOperationAreaController {
    @Autowired
    private IEbOperationAreaService operationAreaService;

    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Autowired
    private EbOperationAreaMapper operationAreaMapper;


    @ApiOperation("获取运营区")
    @GetMapping("/get")
    @Deprecated
    public AjaxResult<OperationAreaVo> get() {
        OperationAreaVo operationAreaVo = operationAreaMapper.getArea(LocalMapUtil.getAgentInfo().getAreaId());
        JSONObject param = new JSONObject();
        BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(LocalMapUtil.getAgentInfo().getId(), operationAreaVo.getId());
        param.put("userNeedWearHelmet", bikeAreaParam.getUserNeedWearHelmet());
        operationAreaVo.setParam(param);
        return AjaxResult.success(operationAreaVo);
    }


    @ApiOperation("定位运营区(无需登录)")
    @PostMapping("/location")
    public AjaxResult<OperationAreaVo> get(@RequestBody MemberLocationForm memberLocationForm) {
        OperationAreaVo operationAreaVo = operationAreaService.getLocationArea(memberLocationForm.getLng(), memberLocationForm.getLat());
        if (operationAreaVo == null) {
            return AjaxResult.success("没有找到运营区");
        }
        JSONObject param = new JSONObject();
        BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(LocalMapUtil.getAgentInfo().getId(), operationAreaVo.getId());
        param.put("userNeedWearHelmet", bikeAreaParam.getUserNeedWearHelmet());
        operationAreaVo.setParam(param);
        return AjaxResult.success(operationAreaVo);
    }


    @ApiOperation("定位运营区-改成get接口-获取运营区全部参数(无需登录)")
    @GetMapping("/location/get")
    public AjaxResult<OperationAreaVo> getArea(MemberLocationForm memberLocationForm) {
        OperationAreaVo operationAreaVo = operationAreaService.getLocationArea(memberLocationForm.getLng(), memberLocationForm.getLat());
        if (operationAreaVo == null) {
            return AjaxResult.success("没有找到运营区");
        }
        JSONObject param = new JSONObject();
        BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(LocalMapUtil.getAgentInfo().getId(), operationAreaVo.getId());
        String userServicePhone="";
        if (StrUtil.isNotBlank(bikeAreaParam.getUserAreaServicePhone())) {
            userServicePhone=bikeAreaParam.getUserAreaServicePhone();
        }
        String[] split = userServicePhone.split(";");
        List<String> list = new ArrayList<>();
        for (int i=0;i< split.length;i++){
            list.add(split[i]);
        }
        //客服电话支持多个数组
        param.put("userServicePhone",list);
        if (StrUtil.isNotBlank(bikeAreaParam.getUserAreaServiceOnlineLink())) {
            param.put("userServiceOnlineLink",bikeAreaParam.getUserAreaServiceOnlineLink());
        }
        if(bikeAreaParam.getUserStaticCloseOrder() != null && bikeAreaParam.getUserStaticCloseOrder() > 0){
            param.put("userStaticCloseOrder",bikeAreaParam.getUserStaticCloseOrder());
        }
        if(StrUtil.isNotBlank(bikeAreaParam.getCustomerAreaServiceDes())) {
            param.put("customerServiceDes", bikeAreaParam.getCustomerAreaServiceDes());
        }
        param.put("userRideShareEnable", bikeAreaParam.getUserRideShareEnable());
        param.put("userRideRentEnable",bikeAreaParam.getUserRideRentEnable());
        param.put("userRideBuyMemberCard", bikeAreaParam.getUserRideBuyMemberCard());
        param.put("userNeedWareHelmetHintText", bikeAreaParam.getUserNeedWareHelmetHintText());
        param.put("userCameraAIStopBike", bikeAreaParam.getUserCameraAIStopBike());
        param.put("userCameraStopAngle", bikeAreaParam.getUserCameraStopAngle());
        param.put("userCameraStopDistance", bikeAreaParam.getUserCameraStopDistance());
        param.put("userCameraStopColor", bikeAreaParam.getUserCameraStopColor());
        param.put("userRideFaceAuthEnabled", bikeAreaParam.getUserRideFaceAuthEnabled());
        param.put("userMiniLoginFaceAuthEnabled", bikeAreaParam.getUserMiniLoginFaceAuthEnabled());
        param.put("userRideRealNameAuthEnabled", bikeAreaParam.getUserRideRealNameAuthEnabled());
        param.put("userMinLoginFaceLifeAuthEnabled", bikeAreaParam.getUserMinLoginFaceLifeAuthEnabled());
        param.put("userBikeHelmetLockStyle", bikeAreaParam.getUserBikeHelmetLockStyle());
        param.put("userRideStopBikeMaxTimesEnabled", bikeAreaParam.getUserRideStopBikeMaxTimesEnabled());
        param.put("userNeedWearHelmet", bikeAreaParam.getUserNeedWearHelmet());
        operationAreaVo.setParam(param);
        return AjaxResult.success(operationAreaVo);
    }
}
